-- Tags: no-fasttest, no-random-settings, no-parallel-replicas
-- - no-fasttest -- requires S3
-- - no-random-settings -- more deterministic prefetch
-- - no-parallel-replicas -- other replicas may do prefetch

-- Disable multibuffer, there is a separate test
SET merge_tree_compact_parts_min_granules_to_multibuffer_read = 129;

{% for min_bytes_for_wide_part in [0, 10e9] %}

{% set table = 'data' +
    ('_wide' if min_bytes_for_wide_part == 0 else '_compact') +
    ''
%}

DROP TABLE IF EXISTS {{ table }};

CREATE TABLE {{ table }}
(
    {% for i in range(1, 10) %}
    c{{ i }} String,
    {% endfor %}
    c0 String
)
ENGINE = MergeTree
PARTITION BY tuple()
ORDER BY ()
SETTINGS
    disk='s3_cache',
    min_bytes_for_wide_part = {{ min_bytes_for_wide_part }},
    serialization_info_version = 'basic',
    min_bytes_for_full_part_storage=0;

INSERT INTO {{ table }} SELECT * FROM generateRandom() LIMIT 400;

SELECT * FROM {{ table }} FORMAT Null SETTINGS allow_prefetched_read_pool_for_remote_filesystem = 0, filesystem_prefetches_limit = 5;
SELECT * FROM {{ table }} FORMAT Null SETTINGS allow_prefetched_read_pool_for_remote_filesystem = 0, filesystem_prefetches_limit = 10;
SELECT * FROM {{ table }} FORMAT Null SETTINGS allow_prefetched_read_pool_for_remote_filesystem = 1, filesystem_prefetches_limit = 5;
SELECT * FROM {{ table }} FORMAT Null SETTINGS allow_prefetched_read_pool_for_remote_filesystem = 1, filesystem_prefetches_limit = 10;

{% endfor %}

SYSTEM FLUSH LOGS query_log;
SELECT normalizeQuery(query), Settings['allow_prefetched_read_pool_for_remote_filesystem'], Settings['filesystem_prefetches_limit'], ProfileEvents['RemoteFSPrefetches'] FROM system.query_log
WHERE current_database = currentDatabase() AND type != 'QueryStart' AND query_kind = 'Select'
ORDER BY event_time_microseconds;
